package net.xiaohanlin.smartmetric.monitor.jvm;

import java.lang.management.ClassLoadingMXBean;
import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.Map;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricSet;

import net.xiaohanlin.smartmetric.metricname.JvmClassLoadingMetricNameEnum;

public class ClassLoadingGaugeSet extends AbstractJvmMetric implements MetricSet {

	private final ClassLoadingMXBean mxBean;

	public ClassLoadingGaugeSet() {
		this(ManagementFactory.getClassLoadingMXBean());
	}

	public ClassLoadingGaugeSet(ClassLoadingMXBean mxBean) {
		this.mxBean = mxBean;
	}

	@Override
	public Map<String, Metric> getMetrics() {
		final Map<String, Metric> gauges = new HashMap<String, Metric>();

		gauges.put(getMetricName(JvmClassLoadingMetricNameEnum.jvm_classloader_loaded), new Gauge<Long>() {

			@Override
			public Long getValue() {
				return mxBean.getTotalLoadedClassCount();
			}
		});

		gauges.put(getMetricName(JvmClassLoadingMetricNameEnum.jvm_classloader_unloaded), new Gauge<Long>() {

			@Override
			public Long getValue() {
				return mxBean.getUnloadedClassCount();
			}
		});

		return gauges;
	}

}
